package cn.tqfeiyang.smartadmin.business.organization.service;

import cn.tqfeiyang.smartadmin.business.organization.entity.EmployeeEntity;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * 员工列表服务
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@Service
public class EmployeeListService {

    /**
     * 员工缓存服务
     */
    @Resource
    private EmployeeCacheService employeeCacheService;

    /**
     * 查询员工列表
     *
     * @return 员工列表
     */
    public List<EmployeeEntity> getEmployeeList() {
        return employeeCacheService.getEmployeeList();
    }

    /**
     * 查询员工map
     *
     * @return 员工map
     */
    public Map<Long, EmployeeEntity> getEmployeeMap() {
        return employeeCacheService.getEmployeeMap();
    }

    /**
     * 根据员工id查询员工信息
     *
     * @param employeeId 员工id
     * @return 员工信息，如果不存在则返回null
     */
    public EmployeeEntity findEmployeeByEmployeeId(Long employeeId) {
        Assert.notNull(employeeId, "employeeId不能为空");
        return employeeCacheService.findEmployeeByEmployeeId(employeeId);
    }


    /**
     * 根据登录账号查询员工信息
     *
     * @param loginName 登录账号
     * @return 员工信息，如果不存在则返回null
     */
    public EmployeeEntity findEmployeeByLoginName(String loginName) {
        Assert.notNull(loginName, "loginName不能为空");
        return getEmployeeList().stream()
                .filter(entity -> loginName.equals(entity.getLoginName()))
                .findFirst()
                .orElse(null);
    }

    /**
     * 根据电子邮件查询员工信息
     *
     * @param email 电子邮件
     * @return 员工信息，如果不存在则返回null
     */
    public EmployeeEntity findEmployeeByEmail(String email) {
        Assert.notNull(email, "email不能为空");
        return getEmployeeList().stream()
                .filter(entity -> email.equals(entity.getEmail()))
                .findFirst()
                .orElse(null);
    }

    /**
     * 根据电话查询员工信息
     *
     * @param phone 电话
     * @return 员工信息，如果不存在则返回null
     */
    public EmployeeEntity findEmployeeByPhone(String phone) {
        Assert.notNull(phone, "phone不能为空");
        return getEmployeeList().stream()
                .filter(entity -> phone.equals(entity.getPhone()))
                .findFirst()
                .orElse(null);
    }
}
